package com.nstar.base.util
{
	import com.nstar.base.lang.Stack;
	
	/**
	 * 历史记录管理器
	 * 采用单例模式，使用时请用getInstance()方法得到实例
	 * 
	 * 使用方法：
	 * step 1: 首先通过getInstance()得到实例
	 * step 2: 新建IUndo对象，用pushUndo(undo:IUndo)方法，将该对象纳入管理器管理
	 * step 3: 做撤销操作时，直接使用undo()方法
	 * 
	 * @author 李嘉陵
	 * @since 2007-05-09
	 */
	public class HistoryManager
	{
		private var undos:Stack;
		private static var inst:HistoryManager;
		
		public function HistoryManager(length:int = 10) 
		{
			undos = new Stack(length);
		}
		
		public static function getInstance() : HistoryManager 
		{
			if(inst == null) {
				inst = new HistoryManager();
			}
			
			return inst;
		}
		
		/**
		 * 将Undo操作入栈
		 */
		public function pushUndo(undo:IUndo) : void 
		{
			undos.push(undo);
		}
		
		/**
		 * 执行Undo操作
		 */
		public function undo() : void 
		{
			if(undos.isNotEmpty()) {
				var undo:IUndo = IUndo(undos.pop());
				undo.undo();
			}
		}
		
		public function isEmpty() : Boolean 
		{
			return undos.isEmpty();
		}
		
		public function isNotEmpyt() : Boolean
		{
			return undos.isNotEmpty();
		}
		
		public function size() : int
		{
			return undos.size;
		}
		
	}
}